import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/internal/operators/map';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';

import { MonitorVo, MonitorStatisticsVo, MonitorTreeMap, ChartVo, PageVo } from 'src/app/shared/models';
import { MockApiService } from './api-mock.service';
import { ApiService, JsonResult } from './api.service';
import { HttpParams } from '@angular/common/http';

@Injectable({
    providedIn: 'root'
})
export class MonitorService {
    private _monitorList: BehaviorSubject<Array<MonitorVo>> = new BehaviorSubject([]);

    constructor(private mockApiService: MockApiService,
        private apiService: ApiService) { }

    get monitorList$() {
        return this._monitorList.asObservable();
    }

    set monitorList(monitorList: Array<MonitorVo>) {
        this._monitorList.next(monitorList);
    }

    getHistoryMonitorList(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<MonitorVo>> {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`);
        return this.apiService.get('/importWarningInfo/historyWarningList', { params }).pipe(
            map((jsonResult: JsonResult<PageVo<MonitorVo>>) => {
                return jsonResult.data;
            })
        );
    }

    getMonitorTreemap(): Observable<Array<MonitorTreeMap>> {
        return this.apiService.get('/monitor/tsituation').pipe(
            map((jsonResult: JsonResult<Array<MonitorTreeMap>>) => {
                return jsonResult.data;
            })
        );
    }

    getMonitorStatictics(): Observable<MonitorStatisticsVo> {
        return this.apiService.get('/monitor/statistical-list').pipe(
            map((jsonResult: JsonResult<MonitorStatisticsVo>) => {
                return jsonResult.data;
            })
        );
    }

    getMonitorHighTrend(): Observable<ChartVo> {
        return this.apiService.get('/monitor/highRiskTrend').pipe(
            map((jsonResult: JsonResult<ChartVo>) => {
                return jsonResult.data;
            })
        );
    }
}
